package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.UserCartDao;
import ex.com.jm.food.entity.Company;
import ex.com.jm.food.entity.FoodCart;
import ex.com.jm.food.entity.Menu;
import ex.com.jm.food.entity.Streets;

public class UserCartDaoImpl extends HibernateDaoSupport implements UserCartDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<FoodCart> getCarts(final String userId) {
		// TODO Auto-generated method stub
		Object carts=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from FoodCart where userId='"+userId+"'";
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<FoodCart>) carts;
	}

	@Override
	public Object saveCart(FoodCart foodCart) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(foodCart);
	}

	@Override
	public void delCart(String cartId) {
		// TODO Auto-generated method stub
        this.getHibernateTemplate().delete(this.getHibernateTemplate().get(FoodCart.class, cartId));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateCart(final String cartId,final int num) {
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update FoodCart set menuNum="+num+" where cartId='"+cartId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if (count==1) {
			return true;
		}
		return false;
		// TODO Auto-generated method stub

	}

	@Override
	public Menu getMenu(String menuId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Menu.class, menuId);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public FoodCart checkFoodCart(final String menuId,final String userId) {
		// TODO Auto-generated method stub
		FoodCart foodCart=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from FoodCart where userId='"+userId+"' and menuId='"+menuId+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return foodCart;
	}

	@Override
	public Company getCompany(String comId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Company.class, comId);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Streets> getStreets() {
		// TODO Auto-generated method stub
		Object streets=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from Streets");
				return query.list();
			}
		});
		return (List<Streets>) streets;
	}

}
